const USER_KEY = 'userId' //用户信息
const ACCESS_TOKEN_KEY = "access_token" //token
const ACCESS_TOKEN_HEADER_KEY = "access_token_header" //token_header
import {
	jwtDecode
} from "jwt-decode";
/**
 * 缓存用户信息
 * @param {Object} val
 */
const setUser = (val) => {
	uni.setStorageSync(USER_KEY, val)
}

/** 获取用户信息
 * @param {Object} val
 */
const getUser = () => {
	const userInfo = uni.getStorageSync(USER_KEY) || {}
	if (!userInfo.avator) {
		userInfo.avator = '/static/avator.png'
	}

	return userInfo;
}

/**
 * 缓存token信息
 * @param {Object} val
 */
const setToken = (val) => {
	// 缓存token信息
	uni.setStorageSync(ACCESS_TOKEN_KEY, val)
	// 解析token信息
	var decoded = jwtDecode(val);
	const payload = decoded.payload
	// 过期时间payload,freshTime 
	// 缓存用户信息
	setUser(JSON.parse(payload.info))
	// 缓存 tokenHeader信息
	let header = {}
	header[payload.k] = val
	uni.setStorageSync(ACCESS_TOKEN_HEADER_KEY, header)
}

/** 获取token信息
 */
const getToken = () => {
	return uni.getStorageSync(ACCESS_TOKEN_KEY)
}

/** 获取tokenHeader信息
 */
const getTokenHeader = () => {
	return {
		'No-Sign': 'N'
	} //测试专用
	// return uni.getStorageSync(ACCESS_TOKEN_HEADER_KEY)||{}
}
export default {
	setUser,
	getUser,
	setToken,
	getToken,
	getTokenHeader
}